package pt.iscte.daam.model;

import java.io.Serializable;
import java.util.Calendar;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import pt.iscte.daam.queries.Queries;

@Table(name = "Quarto")
@Entity
@NamedQueries({
	@NamedQuery(name = Queries.SELECT_QUARTOS, 
				query = "SELECT q FROM Quarto q LEFT JOIN FETCH q.imovel ORDER BY q.dataCriacao DESC"),
	@NamedQuery(name=Queries.SELECT_QUARTO_POR_ID,
    			query="SELECT q FROM Quarto q WHERE q.id = :id"),
})
public class Quarto implements Serializable{

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1222240440793179823L;
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "IdQuarto")
	private Long id;
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "Imovel")
	private Imovel imovel;

	@Column(name = "DataCriacao")
	private Calendar dataCriacao;
	
	@Column(name = "Preco")
	private float preco;
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Imovel getImovel() {
		return imovel;
	}

	public void setImovel(Imovel imovel) {
		this.imovel = imovel;
		if(! imovel.getQuartos().contains(this)){
			imovel.getQuartos().add(this);
		}
	}

	public Calendar getDataCriacao() {
		return dataCriacao;
	}

	public void setDataCriacao(Calendar dataCriacao) {
		this.dataCriacao = dataCriacao;
	}

	public float getPreco() {
		return preco;
	}

	public void setPreco(float preco) {
		this.preco = preco;
	}
}
